package com.sanjiahantang.youjiarole.module.programmeprice.views.impl;

import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.TagGroup;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.me.order.views.OrderView;
import com.sanjiahantang.youjiarole.module.programmeprice.beans.ProgramOrderListBean;
import com.sanjiahantang.youjiarole.module.programmeprice.presenters.ProgramPricePresenter;
import com.sanjiahantang.youjiarole.module.programmeprice.views.ProgramPriceView;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zy on 2017/11/21.
 */

public class ProgramOrderActivity extends BaseMvpActivity<ProgramPriceView, ProgramPricePresenter> implements ProgramPriceView, View.OnClickListener {

    private View panel1, panel2, panel3, panel4;

    private TextView txtPlanPrice, txtOrderPrice;
    private ProgramOrderListBean.Data orderData;
    private ListView listIncrease;
    private int ppId;
    private int type;
    private String version;

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public ProgramPricePresenter initPresenter() {
        return new ProgramPricePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_program_order_list);
        ppId = getIntent().getIntExtra(KEY_ID, 0);
        type = getIntent().getIntExtra(KEY_TYPE, 0);
        version = getIntent().getStringExtra(KEY_VERSION);
        if (ppId == 0) {
            Utils.showToast($this, "订单找不到了");
            finish();
            return;
        }
        txtPlanPrice = (TextView) findViewById(R.id.txtPlanPrice);
        txtOrderPrice = (TextView) findViewById(R.id.txtOrderPrice);
        panel1 = findViewById(R.id.panel1);
        panel2 = findViewById(R.id.panel2);
        panel3 = findViewById(R.id.panel3);
        panel4 = findViewById(R.id.panel4);

        panel1.setVisibility(View.GONE);
        panel2.setVisibility(View.GONE);
        panel3.setVisibility(View.GONE);
        panel4.setVisibility(View.GONE);
        listIncrease = (ListView) findViewById(R.id.listIncrease);

        TextView title = (TextView) findViewById(R.id.title);
        title.setText("方案报价清单");
        if (type == TYPE_INCREASE) {
            title.setText("增项报价清单");
        }
        findViewById(R.id.fl_Left).setOnClickListener(this);
    }

    @Override
    public void initData() {
        presenter.findPlanInfo($this, ppId, version, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (content != null && content instanceof ProgramOrderListBean.Data) {
                    orderData = (ProgramOrderListBean.Data) content;
                    txtPlanPrice.setText(String.format("%.2f",orderData.getPlanPrice()));
                    if (orderData.getPlanInfo() == null) {
                        Utils.showToast($this, "订单数据错误");
                        finish();
                        return;
                    }
                    txtOrderPrice.setText(String.format("%.2f",orderData.getPlanInfo().getOrderTotalMoney()));
                    if (orderData.getPlanInfo().getOrderServiceRaDtoList() != null) {
                        for (int i = 0; i < orderData.getPlanInfo().getOrderServiceRaDtoList().size(); i++) {
                            fulfillPanel(panel1, orderData.getPlanInfo().getOrderServiceRaDtoList().get(i), "施工", R.layout.item_program_nopic_n);
                            panel1.setVisibility(View.VISIBLE);
                        }
                    }
                    if (orderData.getPlanInfo().getOrderInfoGoodsRaDtoList() != null) {
                        for (int i = 0; i < orderData.getPlanInfo().getOrderInfoGoodsRaDtoList().size(); i++) {
                            if (orderData.getPlanInfo().getOrderInfoGoodsRaDtoList().get(i).getOrderType() == 0) {
                                fulfillPanel(panel2, orderData.getPlanInfo().getOrderInfoGoodsRaDtoList().get(i), "主材", R.layout.item_program_nopic_p);
                                panel2.setVisibility(View.VISIBLE);
                            } else if (orderData.getPlanInfo().getOrderInfoGoodsRaDtoList().get(i).getOrderType() == 1) {
                                fulfillPanel(panel3, orderData.getPlanInfo().getOrderInfoGoodsRaDtoList().get(i), "家具", R.layout.item_program_nopic_p);
                                panel3.setVisibility(View.VISIBLE);
                            } else if (orderData.getPlanInfo().getOrderInfoGoodsRaDtoList().get(i).getOrderType() == 2) {
                                fulfillPanel(panel4, orderData.getPlanInfo().getOrderInfoGoodsRaDtoList().get(i), "装饰", R.layout.item_program_nopic_p);
                                panel4.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                    if (orderData.getOrderIncreaseList() != null) {
                        listIncrease.setAdapter(new QuickAdapter<ProgramOrderListBean.Increase>($this, R.layout.item_text, orderData.getOrderIncreaseList()) {
                            @Override
                            protected void convert(BaseAdapterHelper helper, final ProgramOrderListBean.Increase item) {
                                helper.setTextColor(R.id.text1,COLOR_PRIMARY);
                                helper.setVisible(R.id.ivArcher, true);
                                helper.setText(R.id.text1, item.getOrderIncreaseName());
                                helper.getView().setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        startActivity(new Intent($this, ProgramAddOrderActivity.class)
                                                .putExtra(KEY_ID, item.getOrderIncreaseCode())
                                                .putExtra(KEY_TYPE, TYPE_INCREASE)
                                        );
                                    }
                                });
                            }
                        });
                    }

                }
            }
        });
    }

    private void fulfillPanel(View itemView, final ProgramOrderListBean.Order order, String title, int resId) {
        TextView txtOrderCode = (TextView) itemView.findViewById(R.id.comName);
        TextView txtState = (TextView) itemView.findViewById(R.id.comStates);
        ListView itemList = (ListView) itemView.findViewById(R.id.SG_list);
        TextView txtCount = (TextView) itemView.findViewById(R.id.TotalCount);
        CheckBox checkBox = (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
        TextView txtPrice = (TextView) itemView.findViewById(R.id.TotalPrice);
        //    TextView txtState = itemView.findViewById(R.id.comStates);

        checkBox.setVisibility(View.GONE);


        txtState.setVisibility(View.VISIBLE);
        itemView.findViewById(R.id.countItem).setVisibility(View.VISIBLE);

        String stateName = order.getStatusName();
        switch (order.getStatusId()) {
            case OrderView.TYPE_UN_PAY:
                stateName = "待付款";
                //    itemView.findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                break;
            case OrderView.TYPE_PART_PAY:
                //      itemView.findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                //     itemList.findViewById(R.id.btnCancel).setVisibility(View.GONE);
                break;
            default:
                //    itemView.findViewById(R.id.panelAction).setVisibility(View.GONE);
                break;
        }


        txtOrderCode.setText(title + "  ( " + order.getOrderCode() + " )");
        txtState.setText(stateName);

        if (order.getServiceRecordList() != null) {
            txtCount.setText(String.valueOf(order.getServiceRecordList().size()));
            txtPrice.setText(String.format(" ¥ %.2f" , order.getPayMoney()));
            itemList.setAdapter(new QuickAdapter<ProgramOrderListBean.Service>($this, resId, order.getServiceRecordList()) {
                @Override
                protected void convert(BaseAdapterHelper helper, ProgramOrderListBean.Service item) {
                    helper.getView(R.id.iv_pic).setVisibility(View.GONE);
                    helper.setText(R.id.sg_name, item.getServiceName());
                    helper.setText(R.id.txtPrice, String.format("¥ %.2f" , item.getResult()));
                }
            });

//            itemView.findViewById(R.id.btnPay).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//                }
//            });

//            itemView.findViewById(R.id.btnCancel).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//                }
//            });
        }

        if (order.getGoodsInfoDtoList() != null) {
            int count = 0;
            for (ProgramOrderListBean.Goods item : order.getGoodsInfoDtoList()){
                count += item.getCount();
            }
            txtCount.setText(String.valueOf(count));
            txtPrice.setText(String.format(" ¥ %.2f" , order.getPayMoney()));

            final List<ProgramOrderListBean.Goods> showList = new ArrayList<>();
            final QuickAdapter adapter = new QuickAdapter<ProgramOrderListBean.Goods>($this, resId) {
                @Override
                protected void convert(BaseAdapterHelper helper, ProgramOrderListBean.Goods item) {
                    helper.getView(R.id.iv_icon).setVisibility(View.GONE);
                    helper.setText(R.id.txtMainName, item.getRecordName());
                    helper.setText(R.id.txtMainPrice, String.format(" ¥ %.2f" , item.getPrice()));
                    helper.setText(R.id.txtCount, String.valueOf(item.getCount()));


                    if (item.getMgpiValue() != null) {
                        TagGroup tagGroup = helper.getView(R.id.txtName);
                        tagGroup.setTags(item.getMgpiValue().split(","));
                    }

                    ImageView iv_pic = helper.getView(R.id.iv_pic);
                    Picasso.with(context).load(FileUtils.rePath(item.getUrl()))
                            .placeholder(RES_ID_GOODS_DEFAULT)
                            .into(iv_pic);
                }
            };


            if (order.getGoodsInfoDtoList() != null){
                final TextView tvMore = (TextView) itemView.findViewById(R.id.tvMore);
                if (order.getGoodsInfoDtoList().size() > SHOW_SIZE){
                    showList.addAll(order.getGoodsInfoDtoList().subList(0,SHOW_SIZE));
                    tvMore.setVisibility(View.VISIBLE);
                    tvMore.setTag(R.id.decode,1);
                    tvMore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Object tag = view.getTag(R.id.decode);
                            if (tag != null && tag instanceof Integer){
                                int type = (int) tag;
                                if (type == 1){
                                    tvMore.setTag(R.id.decode,2);
                                    tvMore.setText("收起列表");
                                    adapter.clear();
                                    adapter.addAll(order.getGoodsInfoDtoList());
                                }else{
                                    tvMore.setTag(R.id.decode,1);
                                    tvMore.setText("查看全部");
                                    adapter.clear();
                                    adapter.addAll(order.getGoodsInfoDtoList().subList(0,SHOW_SIZE));
                                }
                            }
                        }
                    });
                }else{
                    showList.addAll(order.getGoodsInfoDtoList());
                    tvMore.setVisibility(View.GONE);
                }
            }

            adapter.clear();
            adapter.addAll(showList);
            itemList.setAdapter(adapter);

//            itemView.findViewById(R.id.btnPay).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Bundle b = new Bundle();
//                    ShopListBean.Data goods = new ShopListBean.Data();
//                    goods.setOrderCode(order.getOrderCode());
//                    b.putSerializable("goods",goods);
//                    startActivity(new Intent($this, OrderActivity.class).putExtra(KEY_PARAM,b));
//                }
//            });
//
//            itemView.findViewById(R.id.btnCancel).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Bundle b = new Bundle();
//                    ShopListBean.Data goods = new ShopListBean.Data();
//                    goods.setOrderCode(order.getOrderCode());
//                    b.putSerializable("goods",goods);
//                    startActivity(new Intent($this, OrderActivity.class).putExtra(KEY_PARAM,b));
//                }
//            });

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
        }
    }
}
